package com.kuang.web.admin;

import com.kuang.pojo.Type;
import com.kuang.service.BlogService;
import com.kuang.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class TypeController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @RequestMapping("/totype")
    public String totype(Model model) {
        List<Type> typeList = typeService.selectAllType();
        model.addAttribute("typelist", typeList);
        return "admin/category-mgr";
    }

    @RequestMapping("/addtype")
    @ResponseBody
    public String addtype(String name) {
//        System.out.println(name);
        Type type = typeService.selectTypeByName(name);
        if (type != null) {  // 通过分类的名字查询，如果有type对象说明有同样的对象
            return "error";
        } else {
            int i = typeService.insertType(name, new Date(), new Date());
            if (i > 0) {
                return "success";
            } else {
                return "error";
            }
        }
//        return "11";
    }

    @RequestMapping("/deletetype")
    @ResponseBody
    public String deletetype(String typeid) {
//        System.out.println(typeid);
//        System.out.println("test");
//        blogService.sele
        int i = typeService.deleteTypeById(Integer.parseInt(typeid));
//        删除分类id的时候，同时要将博客表中对应分类的id修改为null 要不然后面会根据这个已经消失的分类id在分类表查询会爆出异常
        int i1 = blogService.updateBlogTypeIdByTypeId(Integer.parseInt(typeid));
        if (i > 0 && i1 > 0) {
            return "success";
        } else {
            return "error";
        }
    }


    @RequestMapping("/modifytype")
    @ResponseBody
    public String modifytype(String typeid, String typename) {
        int i = typeService.updateTyepById(Integer.parseInt(typeid), typename, new Date());
        if (i > 0) {
            return "success";

        } else {
            return "error";
        }
    }

    @RequestMapping("/totypepage")
    public String totypepage(Model model, @RequestParam(defaultValue = "1") int pageindex) {
        int pagesize = 6;  //  每页显示6个
        int countType = typeService.countType();
        List<Type> typeList = typeService.selectTypePage((pageindex - 1) * pagesize, pagesize);
        int pageAll = countType % pagesize == 0 ? countType / pagesize : (countType / pagesize) + 1;
//        System.out.println(pageAll);
//        System.out.println("========" + (11 % 6));
        model.addAttribute("typelist", typeList);
        model.addAttribute("counttype", countType);
//        System.out.println(countType);
        for (Type type : typeList) {
            System.out.println(type);
        }
        model.addAttribute("pageAll", pageAll);
        model.addAttribute("pageindex", pageindex);
        return "admin/category-mgr";

    }
}
